import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
var App = getApp();

Component({
  data: {
    userInfo: {},
    payType: ''
  },
  properties: {
    order: {
      type: Object,
      value: {}
    },
    show: {
      type: Boolean,
      value: false
    },
  },
  attached: function () {
    const _this = this
    _this.setData({ order: _this.properties.order});

  }, // 此处attached的声明会被lifetimes字段中的声明覆盖

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      const _this = this

    },
    hide: function () {},
    resize: function () {},
  },

  methods: {
    onSubmit: function (res) {
      const _this = this
      var order = this.data.order
      if(order.status==1){
        App.globalData.userModel.userInfo().then(json => {
          _this.setData({
            userInfo: json.data,
            show: true
          })
        })
      }
      
    },

    closePay: function (res) {
      this.setData({ show: false})
    },

    payment(event) {
      var _this = this
      if (!this.data.payType) {
        wx.showToast({
          title: '请选择支付方式',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false,

        });
        return
      }
      App.globalData.serviceModel.payment({
        data: {
          id: _this.data.order.id,
          pay_type: _this.data.payType
        }
      }).then(json => {
        _this.setData({ show: false})
        if (_this.data.payType==1){

          wx.requestPayment({
            timeStamp: json.data.timeStamp,
            nonceStr: json.data.nonceStr,
            package: json.data.package,
            signType: json.data.signType,
            paySign: json.data.paySign,
            success(res) {
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                image: '',
                duration: 1500,
                mask: false,
              });
              _this.updateOrder()
            }
          })

        }
        _this.updateOrder()
      })

    },


    onChange(event) {
      this.setData({ radio: event.detail});
    },

    onClick(event) {
      const { name} = event.currentTarget.dataset;
      this.setData({ payType: name});
    },

    /**
     * 更新订单信息
     */
    updateOrder() {
      var _this = this
      App.globalData.serviceModel.orderDetail({
        data: { id: _this.data.order.id}
      }).then(json => {
        json.data.category_group_price = parseFloat(json.data.category_group_price)
        _this.setData({ order: json.data})
      })
    }
  }
})